package com.retailshop.shopuser.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qiniu.util.Json;
import com.retailshop.shopuser.common.InterfaceVer;
import com.retailshop.shopuser.common.MessageConstants;
import com.retailshop.shopuser.common.Response;
import com.retailshop.shopuser.entity.RedisUserNo;
import com.retailshop.shopuser.entity.UserAndUserNoInfo;
import com.retailshop.shopuser.entity.UserData;
import com.retailshop.shopuser.entity.UserNo;
import com.retailshop.shopuser.service.UserDataService;
import com.retailshop.shopuser.service.UserNoService;
import com.retailshop.shopuser.service.UserRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/User")
public class UserController {

    @Autowired
    UserDataService userDataService;

    @Autowired
    UserNoService userNoService;




    /**
     * 读取所有数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/List", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response AdminList(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);
                //将接收到的数据转化为对象
                UserData userData = JSON.parseObject(res, new TypeReference<UserData>() {
                });

                if (!res.contains("state")) {
                    userData.setState(-1);
                }

                if (res.contains("no")) {
                    userData.setNo(userData.getNo().replace(" ", "%"));
                }

                if (res.contains("name")) {
                    userData.setName(userData.getName().replace(" ", "%"));
                }

                if (res.contains("phone")) {
                    userData.setPhone(userData.getPhone().replace(" ", "%"));
                }

                PageHelper.startPage(userData.getPage(), userData.getLimit());

                //读取数据表
                List<UserData> listUserData = userDataService.selectUserList(userData);
                if (listUserData != null) {

                    PageInfo<UserData> pageInfo = new PageInfo<>(listUserData);
                    int count = (int) pageInfo.getTotal();
                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, ex.getMessage(), 0, "读取数据错误", request);
            }
        }
    }

    /**
     * 读取数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Self", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Self(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);
                //将接收到的数据转化为对象
                UserNo userNo1 = JSON.parseObject(res, new TypeReference<UserNo>() {
                });

                UserNo userNo = userNoService.selectByPrimaryKey(userNo1.getId());
                if (userNo != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, userNo, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误2", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

    /**
     * 读取数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Self1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Self1(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);
                //将接收到的数据转化为对象
                UserNo userNo1 = JSON.parseObject(res, new TypeReference<UserNo>() {
                });

                UserAndUserNoInfo userNo = userNoService.selectUserAndUserNoByNo(userNo1.getNo());
                if (userNo != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, userNo, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误2", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

    /**
     * 读取数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/SelfShare", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response SelfShare(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);
                //将接收到的数据转化为对象
                UserNo userNo1 = JSON.parseObject(res, new TypeReference<UserNo>() {
                });
                System.out.println(userNo1.getId());
                UserNo userNo = userNoService.selectByPrimaryKey(userNo1.getId());
                if (userNo != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, userNo, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误2", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

    /**
     * 读取数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOwner", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response getOwner(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);
                //将接收到的数据转化为对象
                UserNo userNo1 = JSON.parseObject(res, new TypeReference<UserNo>() {});
                System.out.println(userNo1.getId());
                UserNo userNo = userNoService.getOwner(userNo1.getId());
                if (userNo != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, userNo, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, 0, 0, "没有店主", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }

    }

    /**
     * 读取数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getVera", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response getVera(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);
                //将接收到的数据转化为对象
                UserNo userNo1 = JSON.parseObject(res, new TypeReference<UserNo>() {});
                System.out.println(userNo1.getId());
                UserNo userNo = userNoService.getVera(userNo1.getId());
                if (userNo != null) {
                    return new Response(MessageConstants.MSG_SUCCESS, userNo, 1, "", request);
                } else {
                    return new Response(MessageConstants.MSG_SUCCESS, 0, 0, "没有芦荟屋", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }

    }

   //根据编号查询用户数据
    @ResponseBody
    @RequestMapping(value = "/getUserAndNoInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response getUserAndNoInfo(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                JSONObject jsonObject= JSONObject.parseObject(res);
                String userNo1=(String) jsonObject.get("userNo");
                int AID  = Integer.parseInt(map.get("AID").toString());
                UserAndUserNoInfo userNo = userNoService.selectUserAndUserNoByNo(userNo1);
                return new Response(MessageConstants.MSG_SUCCESS, userNo, 1, "", request);
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "读取数据错误1", request);
            }
        }
    }

    /**
     * 编辑数据
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/Set", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response Set(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID = Integer.parseInt(map.get("AID").toString());
                System.out.println("AID:" + AID);
                //将接收到的数据转化为对象
                UserNo userNo = JSON.parseObject(res, new TypeReference<UserNo>() {
                });
                int row = userNoService.updateByPrimaryKeySelective(userNo);
                if (row > 0) {
                    return new Response(MessageConstants.MSG_SUCCESS, userNo, row, "", request);
                } else {
                    return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "更新数据错误B", request);
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
                return new Response(MessageConstants.MSG_ACCESS_DENY, null, 0, "更新数据错误A", request);
            }
        }
    }

    @ResponseBody
    @RequestMapping(value = "/selectSupplier", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response selectSupplier(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
        List<UserNo> userNoList=new ArrayList<>();
        try {
            userNoList=userNoService.getSupplier();
            return new Response(MessageConstants.MSG_SUCCESS, userNoList, 1, "", request);
        }catch (Exception w){
            return new Response(MessageConstants.MSG_ACCESS_DENY, userNoList, 0, "获取数据错误A", request);
        }
    }


}